import Router from 'vue-router'
/* Layout */
import Layout from '@/layout'

/* Router Modules */
import displayRouter from '@/router/modules/display'
import configRouter from '@/router/modules/config'
// 重写push方法，避免版本不匹配报错
const originalPush = Router.prototype.push
Router.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) {
    return originalPush.call(this, location, onResolve, onReject)
  }
  return originalPush.call(this, location).catch(err => err)
}

export const constantRoutes = [{
  path: '/redirect', component: Layout, hidden: true, children: [{
    path: '/redirect/:path(.*)', component: (resolve) => require(['@/views/redirect/index'], resolve)
  }]
}, {
  path: '/login', component: (resolve) => require(['@/views/login/index'], resolve), hidden: true
}, {
  path: '/auth-redirect', component: (resolve) => require(['@/views/login/auth-redirect'], resolve), hidden: true
}, {
  path: '/404', component: (resolve) => require(['@/views/error-page/404'], resolve), hidden: true
}, {
  path: '/401', component: (resolve) => require(['@/views/error-page/401'], resolve), hidden: true
}, {
  path: '/', component: Layout, redirect: '/displayBusinessList', children: []
}]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [/** when your routing map is too long, you can split it into small modules **/
  configRouter, displayRouter,
  
  {path: '*', redirect: '/404', hidden: true}]

const createRouter = () => new Router({
  scrollBehavior: () => ({y: 0}), routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export function selfaddRoutes(params) {
  router.matcher = new Router().matcher
  router.addRoutes(params)
}

export default router
